//
//  File.swift
//  CalendarShared
//
//  Created by xxs on 2025/7/14.
//

import Foundation
import SwiftUI

public struct FontModel: Codable, RawRepresentable {
    public var name: String
    public var size: CGFloat

    public var font: Font {
        Font.custom(name, size: size)
    }

    #if os(macOS)
    public var nsFont: NSFont {
        NSFont(name: name, size: size) ?? NSFont.systemFont(ofSize: size)
    }
    #else
    public var uiFont: UIFont {
        UIFont(name: name, size: size) ?? UIFont.systemFont(ofSize: size)
    }
    #endif

    public var rawValue: String {
        """
        {
            "name": "\(name)",
            "size": \(size)
        }
        """
    }

    init(name: String, size: CGFloat) {
        self.name = name
        self.size = size
    }

    public init?(rawValue: String) {
        guard let data = rawValue.data(using: .utf8), let obj = try? JSONDecoder().decode(Self.self, from: data) else { return nil }
        self = obj
    }
}
